package com.wwp.tbshop.model.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wwp.tbshop.R;
import com.wwp.tbshop.activity.ProductDetailActivity;
import com.wwp.tbshop.activity.SaleNewsActivity;
import com.wwp.tbshop.model.bean.SaleNewsBean;
import com.wwp.tbshop.util.Constant;
import com.wwp.tbshop.util.UiUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by ${wenping} on 6/16/2017.
 */

public class SaleNewsRVAdapter extends RecyclerView.Adapter {

    private Context mContext;
    SaleNewsActivity mSaleNewsActivity;
    public SaleNewsRVAdapter(Context context,SaleNewsActivity saleNewsActivity) {
        this.mContext = context;
        mSaleNewsActivity = saleNewsActivity;
    }

    List<SaleNewsBean.TopicBean> mSalesNewsLis = new ArrayList<>();

    public void setSalesNewsData(List<SaleNewsBean.TopicBean> salesNewsBeen) {
        mSalesNewsLis = salesNewsBeen;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_goods_salenews, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.setData(mSalesNewsLis.get(position));
    }

    @Override
    public int getItemCount() {
        if (mSalesNewsLis!=null) {
            return mSalesNewsLis.size();
        }
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.item_goods_salenews_iv)
        ImageView mItemGoodsSalenewsIv;
        @Bind(R.id.item_goods_salenews_iv_name)
        TextView mItemGoodsSalenewsIvName;
        private SaleNewsBean.TopicBean mData;
        int i=0;
        private SaleNewsBean.TopicBean mTopicBean;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pId = mTopicBean.getId();
                    Intent intent = new Intent(mContext, ProductDetailActivity.class);
                    intent.putExtra("pId",pId);
                    mContext.startActivity(intent);
                }
            });
        }

        public void setData(SaleNewsBean.TopicBean data) {
            mTopicBean = data;
            mData = data;
            Picasso.with(UiUtil.getContext())
                    .load(Constant.HOST+data.getPic())
                    .placeholder(R.drawable.pic_default_grid)
                    .error(R.drawable.ic_error_page)
                    .into(mItemGoodsSalenewsIv);
            mItemGoodsSalenewsIvName.setText(data.getName());

            /*switch(i)
            {
                case 0:
                    mItemGoodsSalenewsIvName.setText("优雅毛衣");
                    break;
                case 1:
                    mItemGoodsSalenewsIvName.setText("精品皮鞋");
                    break;
                case 2:
                    mItemGoodsSalenewsIvName.setText("美女壁纸");
                    break;
                case 3:
                    mItemGoodsSalenewsIvName.setText("优雅毛衣");
                    break;
                case 4:
                    mItemGoodsSalenewsIvName.setText("优雅毛衣");
                    break;
                case 5:
                    mItemGoodsSalenewsIvName.setText("优雅毛衣");
                    break;
                case 6:
                    mItemGoodsSalenewsIvName.setText("优雅毛衣");
                    break;
                case 7:
                    mItemGoodsSalenewsIvName.setText("优雅毛衣");
                    break;
                default:
                    System.out.println("other");
            }
            i++;*/
        }
    }
}
